package com.heo.music.player.config;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;

import javax.validation.constraints.NotNull;
import java.io.File;
import java.nio.file.Paths;
import java.util.Arrays;

/**
 * created on 2024-05-11 09:35
 *
 * @author 星天无极
 **/
@ConfigurationProperties(prefix = "music.storage")
@Component
@Data
@Slf4j
@Validated
public class MusicStorageConfig implements InitializingBean {

    @NotNull(message = "音乐存储路径不能为空")
    private String path;

    private String musicDir = "music";

    private String coverDir = "cover";

    private String lyricsDir = "lyrics";

    @Override
    public void afterPropertiesSet() throws Exception {
        for (String subPath : Arrays.asList(musicDir, coverDir, lyricsDir)) {
            File file = Paths.get(path, subPath).toFile();
            if (!file.exists()) {
                log.info("创建路径 {}", file.getAbsolutePath());
                if (!file.mkdirs()) {
                    throw new Exception("创建目录失败," + file.getAbsolutePath());
                }
            }
        }
    }
}
